<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if (defined('__ROOT__')) {
    define('__ROOT__', dirname(dirname(__FILE__)));
}

/**
 * @author lukas
 *
 *
 */
class CPT_Controller extends CI_Controller {

    private     $layout_view    = 'template/main';                              #the template file that is included in every view
    private     $content_view   = '';                                           #the content view that is loaded
    private     $sidebar_view   = '';                                           #the sidebar view that is loaded
    protected   $view_data      = array();
    protected   $sidebar_data   = array();
    protected   $headline       = 'No headline!';

    public function __construct() {
        parent::__construct();
    }

    protected function set_layout_view($view) {
        $this->layout_view = $view;
    }

    protected function get_layout_view() {
        return $this->layout_view;
    }

    protected function get_content_view() {
        return $this->content_view;
    }

    protected function get_sidebar_view(){
        return $this->sidebar_view;
    }
    
    protected function set_sidebar_view($view){
        $this->sidebar_view = $view;
    }
    
    protected function set_content_view($view) {
        $this->content_view = $view;
    }

    public function _output($output) {
        $data['headline']   = $this->headline;
        
        //build the view name depending on controller name and method like (views/class/method)
        //if content_view isn't overwritten by the controller
        if (FALSE !== $this->content_view && empty($this->content_view)) {
            $this->content_view = $this->router->class . '/' . $this->router->method;
        }
        if($this->sidebar_view !== FALSE && empty($this->sidebar_view)){
            $this->sidebar_view = $this->router->class . '/sidebar/' . $this->router->method;
        }

        //render the content view
        $data['yield'] = FALSE;
        if (file_exists(APPPATH . 'views/' . $this->content_view . EXT)) {                        #does the view file exist? 
            $data['yield'] = $this->load->view($this->content_view, $this->view_data, TRUE);      #render view and store it in yield
        } else {
            $data['headline']   = 'Error';
            $data['yield']      = 'Content View `' . $this->content_view . '` not found!';
        }
        
        //render the sidebar
        $data['sidebar'] = FALSE;
        if(file_exists(APPPATH . 'views/' . $this->sidebar_view . EXT)){
            $data['sidebar'] = $this->load->view($this->sidebar_view, $this->sidebar_data, TRUE);
        }else
            $data['sidebar'] = '';
        
        //output the stuff directly if no template is set
        if ($this->layout_view) {
            echo $this->load->view($this->layout_view, $data, TRUE);                           
        } else {
            echo $data['yield'];
        }
    }
}

